package com.dcy.znoa.common.util.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 通用返回单个实体
 */
public class JsonCommon<T> implements java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1461199278973391619L;
	private String code;//成功时候是200，失败的时候是其他编码
	private String msg;//失败的时候显示失败原因
	private T data;

	public static final String PARAM_NOT_FOUND_CODE = "305";
	public static final String PARAM_NOT_FOUND_MSG = "缺少入参";

	public static final String SESSION_NOT_FOUND_CODE = "304";
	public static final String SESSION_NOT_FOUND_MSG = "session找不到";

	public static final String NOT_FOUND_CODE = "404";
	public static final String NOT_FOUND_MSG = "找不到";

	public static final String INSERT_ERROR_CODE = "504";
	public static final String INSERT_ERROR_MSG = "插入失败 ";

	public static final String UPDATE_ERROR_CODE = "604";
	public static final String UPDATE_ERROR_MSG = "更新失败 ";

	public static final String DELETE_ERROR_CODE = "704";
	public static final String DELETE_ERROR_MSG = "删除失败 ";

	public static final String SERVICE_ERROR_CODE = "804";
	public static final String SERVICE_ERROR_MSG = "系统内部错误";

	public static final String SUCCESS_CODE = "200";
	public static final String SUCCESS_MSG = "成功";

	public static final JsonCommon SUCCESS = new JsonCommon(SUCCESS_CODE, SUCCESS_MSG);

	public JsonCommon() {
	}

	public JsonCommon(String code) {
		this.code = code;
		switch(code) {
			case NOT_FOUND_CODE:
				this.msg = NOT_FOUND_MSG;
				break;
			case SUCCESS_CODE:
				this.msg = SUCCESS_MSG;
				break;
			case INSERT_ERROR_CODE:
				this.msg = INSERT_ERROR_MSG;
				break;
			case UPDATE_ERROR_CODE:
				this.msg = UPDATE_ERROR_MSG;
				break;
			case DELETE_ERROR_CODE:
				this.msg = DELETE_ERROR_MSG;
				break;
			case PARAM_NOT_FOUND_CODE:
				this.msg = PARAM_NOT_FOUND_MSG;
				break;
			case SESSION_NOT_FOUND_CODE:
				this.msg = SESSION_NOT_FOUND_MSG;
				break;
//			case ResultCode.NOT_FOUND.getCode():
//			case "qwe":
//				this.msg = ResultCode.NOT_FOUND.getMessage();
			default:
				break;
		}
	}

	public JsonCommon(String code,String msg) {
		this.code = code;
		this.msg = msg;
	}
	public JsonCommon(T pojo,String code,String msg) {
		this.code = code;
		this.msg = msg;
		this.data = pojo;
	}

	public JsonCommon(T pojo) {
		this.code = SUCCESS_CODE;
		this.msg = SUCCESS_MSG;
		this.data = pojo;
	}

	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public String toJSONString(){
		return JSON.toJSONStringWithDateFormat(this, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat);
	}
 
}
